package edu.njfu.sas.servlet;

import com.google.gson.Gson;
import edu.njfu.sas.dao.StudentDao;
import edu.njfu.sas.dao.impl.StudentDaoImpl;
import edu.njfu.sas.model.Student;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/updateStudent")
public class UpdataStudentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
        PrintWriter writer=response.getWriter();
        Map map=new HashMap();
        String stuno=request.getParameter("stuno");
        String stuname=request.getParameter("stuname");
        String classes=request.getParameter("classes");
        String gender=request.getParameter("gender");
        String department=request.getParameter("department");
        String tel=request.getParameter("tel");
        String doemno=request.getParameter("doemno");
        String photopath=request.getParameter("photopath");
        Student student=new Student(stuno,stuname,classes,gender,department,tel,doemno,photopath);
        StudentDao studentDao=new StudentDaoImpl();
        boolean result= false;
        try {
            result = studentDao.updateStudent(student);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(result){
          map.put("result","ok");
          map.put("msg","修改成功！");
      }else{
          map.put("result","false");
          map.put("msg","修改失败！");
      }
      writer.write(new Gson().toJson(map));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
    }
}
